import axios from "axios";
import { getToken } from "./utils/setToken";
import { Message } from "element-ui";
const service = axios.create({
  baseURL: "/api", //baseurl会自动夹在请求地址上
  timeout: 3000,
});

//添加请求拦截器
service.interceptors.request.use(
  (config) => {
    //在请求之前干什么（获取并设置token）
    config.headers["token"] = getToken("token");
    return config;
  },
  (error) => {
    return Promise.reject(error);
  },
);

//添加响应拦截器
service.interceptors.response.use(
  (response) => {
    //对响应数据做什么
    let { status, message } = response.data;
    if (status !== 200) {
      Message({ message: message || "error", type: "warning" });
    }
  },
  (error) => {
    return Promise.reject(error);
  },
);

export default service;
